﻿using Beyoung.HelloReading.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Beyoung.HelloReading.WP
{
    class BookModel
    {
        private const string DefaultDirectory = "Books";
        private IsolatedStorageFileStream IsoFileStream;
        private StreamReader streamReader;
        private int lineFontCount;
        private string firstPageContent;
        private readonly string bookpath;
        private int pageIndex;
        private int pageCount;
        private double fileSize;
        private string fileName;
        private string bookName;
        private int pageSize = 1024;
        private byte[] curBuffer;
        private StringBuilder CurrentPageContent;
        private long length;
        private string readRate;
        public int LineFontCount
        {
            get
            {
                return this.lineFontCount;
            }
            set
            {
                this.lineFontCount = value;
            }
        }
        public string FirstPageContent
        {
            get
            {
                return this.firstPageContent;
            }
            set
            {
                this.firstPageContent = value;
            }
        }
        public int PageIndex
        {
            get
            {
                return this.pageIndex;
            }
            set
            {
                this.pageIndex = value;
            }
        }
        public int PageCount
        {
            get
            {
                return this.pageCount;
            }
            set
            {
                this.pageCount = value;
            }
        }
        public double FileSize
        {
            get
            {
                return this.fileSize;
            }
            set
            {
                this.fileSize = value;
            }
        }
        public string FileName
        {
            get
            {
                return this.fileName;
            }
            set
            {
                this.fileName = value;
            }
        }
        public string BookName
        {
            get
            {
                return this.bookName;
            }
            set
            {
                this.bookName = value;
            }
        }
        public int PageSize
        {
            get
            {
                return this.pageSize;
            }
            set
            {
                this.pageSize = value;
            }
        }
        public long Length
        {
            get
            {
                return this.length;
            }
            set
            {
                this.length = value;
            }
        }
        public string ReadRate
        {
            get
            {
                return this.readRate = ((double)(this.pageIndex * this.pageSize) / (double)this.length).ToString("0.00") + " %";
            }
            set
            {
                this.readRate = value;
            }
        }
        public BookModel(string fileName)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists("Books"))
                {
                    store.CreateDirectory("Books");
                }
                if (!store.FileExists(Path.Combine("Books", fileName)))
                {
                    throw new FileNotFoundException();
                }
                this.bookpath = Path.Combine("Books", fileName);
                this.CurrentPageContent = new StringBuilder(this.pageSize);
                this.IsoFileStream = new IsolatedStorageFileStream(this.bookpath, FileMode.Open, store);
                this.streamReader = new StreamReader(this.IsoFileStream, DBCSEncoding.GetDBCSEncoding("GB2312"));
                this.fileSize = (double)this.IsoFileStream.Length / 1048576.0;
                this.length = this.IsoFileStream.Length;
                this.fileName = fileName;
                char[] buf = new char[this.pageSize];
                this.streamReader.ReadBlock(buf, 0, this.pageSize);
                this.firstPageContent = new string(buf);
            }
        }
        public string FirstPage()
        {
            return "";
        }
        public string NextPage()
        {
            int arg_05_0 = Environment.TickCount;
            if (this.streamReader != null)
            {
                this.pageIndex++;
                this.CurrentPageContent.Clear();
                int FontCount = 0;
                int left = 0;
                while (FontCount < this.pageSize)
                {
                    bool isNewLine = false;
                    char[] chr = new char[1];
                    this.streamReader.Read(chr, 0, 1);
                    if (chr[0] == '\n')
                    {
                        FontCount += this.lineFontCount - left;
                        left = 0;
                    }
                    else if (chr[0] != '\r')
                    {

                        //if (chr[0] == '”')
                        //{
                        //    chr[0] = '＂';
                        //}
                        //if (chr[0] == '“')
                        //{
                        //    chr[0] = '＂';
                        //}
                        //if (chr[0] == ' ')
                        //{
                        //    chr[0] = '\u3000';
                        //}
                        //if (chr[0] > 65280 && chr[0] < 65375)
                        //{
                        //    chr[0] = (char)(chr[0] - 65248);
                        //}
                        FontCount++;
                        left++;
                        if (left == this.lineFontCount)
                        {
                            isNewLine = true;
                            left = 0;
                        }

                    }
                    if (!isNewLine)
                    {
                        this.CurrentPageContent.Append(chr[0]);
                    }
                    else
                    {
                        this.CurrentPageContent.Append(chr[0] + Environment.NewLine);
                    }
                }
                return this.CurrentPageContent.ToString();
            }
            return "";
        }
        public string PrePage()
        {
            return "";
        }
        public void Dispose()
        {
            if (this.streamReader != null)
            {
                this.streamReader.Close();
                this.streamReader.Dispose();
            }
            if (this.IsoFileStream != null)
            {
                this.IsoFileStream.Close();
                this.IsoFileStream.Dispose();
            }
        }
    }
}
